from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import cgi
import re
import hashlib
import pickle
import urllib



test = 'http://www.thinkgeek.com/brain/fortune.cgi'
sep = '-------------------------------------------'


def htmlparser(string):
	newstring=""
	htmler=0
    x = 0
    a=0
    while a < (len(string)-15):
        a+=1
        if (string[a]=='t')&(string[a+1]=='e')&(string[a+2]=='>'):#If it's '<blockqoute>'
            htmler += 1
            a += 3
        elif (string[a]=='<')&(string[a+1]=='/')&(string[a+2]=='b'):#If it's '</blockqoute>'
            htmler += 1
        if htmler==1:
            newstring=newstring + string[a] #Adds the other characters
    return newstring

def asc(m):
    return chr(int(m.group(1)))
def regexhtml(string):
#Just a string of regular expressions to remove most common HTML formatting parts
    string = re.sub(r"\\\\'", "'", string)
    string = re.sub(r"&#(\\d\{2,3\});", asc, string)
    string = re.sub(r"&nbsp;", " ", string)
    string = re.sub(r"\\\\n", "\\n", string)
    string = re.sub(r"\\<BR\\>", "\\n", string)
    string = re.sub(r"&gt;", r">", string)
    string = re.sub(r"&lt;", r"<", string)
    string = re.sub(r"&quot;", r'"', string)
    string = re.sub(r"&amp;", r"&", string)
    m = re.search(r"&.+?;", string)
    if m:
        string = regexmore(string)
        
    m = re.search(r"&.+?;", string)
    if m:
        print(m.group())
    return string
def regexmore(string):
    string = re.sub(r"&cent;", chr(162), string)
    string = re.sub(r"&brvbar;", chr(166), string)
    string = re.sub(r"&copy;", chr(169), string)
    string = re.sub(r"&laquo;", chr(169), string)
    string = re.sub(r"&reg;", chr(174), string)
    string = re.sub(r"&macr;", chr(175), string)
    string = re.sub(r"&acute;", chr(180), string)
    string = re.sub(r"&raquo;", chr(187), string)
    string = re.sub(r"&frac14;", chr(188), string)
    string = re.sub(r"&frac12;", chr(189), string)
    string = re.sub(r"&frac34;", chr(190), string)
    string = re.sub(r"&Uuml;", chr(220), string)
    string = re.sub(r"&THORN;", chr(222), string)
    string = re.sub(r"&euml;", chr(235), string)
    string = re.sub(r"&eth;", chr(240), string)
    string = re.sub(r"&ouml;", chr(246), string)
    string = re.sub(r"&oslash;", chr(248), string)
    string = re.sub(r"&uuml;", chr(252), string)
    string = re.sub(r"&thorn;", chr(254), string)
    return string

def urltostring(url): #This is a fairly thin wrapper
    x = regexhtml(htmlparser(urllib.urlopen(url).read()))
    return x[1:(len(x)-1)]
def oldtonewurl(url):
    return url
def fortunes(total=10, url=test):
    counter = 0
    while counter < total:
        text = urltostring(url)
        counter+=1
def t(n=10):
    fortunes(total = n, url=test)
def md5(text):
    return hashlib.md5(pickle.dumps(text)).hexdigest()
def timer(num):
    import time
    start = time.clock()
    t(num)
    end = time.clock()
    diff = end-start
    print('It took '+str(diff)+' seconds(?) to collate '+str(num)+' fortunes.')